import * as R from 'ramda'
import {parse} from './util'
import {STORAGE_TYPE} from './config'

// 包装成数组
export const changeToArr = R.unless(R.is(Array), R.of)

const serviceList = [{
  title: '我的业务',
  list: []
}, {
  title: '党员之家',
  list: [
    {title: '党员组织关系转入', icon: 'xf-folder', link: '/roll-in-1'},
    {title: '党员缴费', icon: 'xf-RMB', link: '/pay'},
    {title: '党员活动报名', icon: 'xf-huodongbaoming', link: '/activities'}
  ]
}, {
  title: '档案管理',
  list: [
    {title: '申请商调函', icon: 'xf-cundang', link: '/apply-1'},
    {title: '档案查询', icon: 'xf-chaxun', link: '/query'},
    {title: '档案转出', icon: 'xf-zhuanchu1', link: '/record-transfer'},
    {title: '开具证明', icon: 'xf-zhengmingwenjian', link: '/certify'},
    {title: '档案借出', icon: 'xf-banli1', link: '/borrow'}
  ]
}, {
  title: '人事代理',
  list: [
    {title: '户口查询', icon: 'xf-search', link: '/residence-search'},
    {title: '户籍关系转入', icon: 'xf-zhuanru', link: '/residence-in'},
    {title: '户籍关系转出', icon: 'xf-zhuanchu', link: '/residence-out'},
    {title: '省医疗保险办理', icon: 'xf-yiliao', link: '/medical-1'},
    {title: '市养老、医疗保险办理', icon: 'xf-yiliao1', link: '/medical-2'},
    {title: '社保查询', icon: 'xf-query1', link: '/security-query'},
    {title: '养老缴费基数变更', icon: 'xf-gengxin', link: '/modify'},
    {title: '职称申报', icon: 'xf-jibenxinxi', link: '/title'}
  ]
}, {
  title: '继续教育',
  list: [
    {title: '继续教育个人信息预录入', icon: 'xf-luru', link: '/education'},
    {title: '继续教育报名', icon: 'xf-renyuanbaoming', link: '/sign-up'},
    {title: '办理合格证明', icon: 'xf-hege', link: '/qualified'}
  ]
}]

export const aServices = getServices(['/apply-1', '/record-transfer', '/borrow', '/medical-1', '/medical-2', '/residence-in', '/residence-out', '/title'])
export const bServices = getServices(['/activities', '/roll-in-1', '/pay', '/modify', '/certify', '/education', '/sign-up', '/qualified'])
export const cServices = getServices(['/query', '/residence-search', '/security-query'])

export function getServices(link) {
  const links = changeToArr(link)
  const arr = []
  links.forEach(item => {
    const it = findService(item)
    if (it) arr.push(it)
  })
  return arr
}

export function findService(link) {
  for (let i = 1; i < serviceList.length; i++) {
    let index = serviceList[i].list.findIndex(item => item.link === link)
    if (index !== -1) {
      return serviceList[i].list[index]
    }
  }
  return ''
}

// 初始化我的业务
const local = parse(localStorage.getItem(STORAGE_TYPE.myService))
if (local) {
  serviceList[0].list = local
} else {
  serviceList[0].list.push(serviceList[1].list[0])
  serviceList[0].list.push(serviceList[2].list[0])
  serviceList[0].list.push(serviceList[3].list[0])
  serviceList[0].list.push(serviceList[4].list[0])
}

export default serviceList
